//
//  SiterBaseNetManager.m
//  sHome
//
//  Created by TracyHenry on 2022/8/15.
//  Copyright © 2022年 SiterWell. All rights reserved.
//
#import "SiterAPI.h"
#import "ErrorModel.h"
#import "AFNetworking.h"
#import "SiterBaseNetManager.h"
@implementation SiterBaseNetManager

+ (id)GETMULLAN:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    
    NSString * lan;
    NSArray *appLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    NSString *languageName = [appLanguages objectAtIndex:0];
    if ([languageName containsString:@"zh"]) {
        lan = @"zh";
    } else if ([languageName containsString:@"de"]) {
        lan = @"de";
    } else if ([languageName containsString:@"fr"]) {
        lan = @"fr";
    } else if ([languageName containsString:@"es"]) {
        lan = @"es";
    }else {
        lan = @"en";
    }
    AFHTTPRequestSerializer *requestSerializer =  [AFJSONRequestSerializer serializer];
    [requestSerializer setValue:lan forHTTPHeaderField:@"Accept-Language"];
    [[Siter sharedInstance] sessionWithDefaultAuthorization].requestSerializer = requestSerializer;
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL.absoluteString);
        !completionHandler?:completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@, %@", task.currentRequest.URL.absoluteString, error);
        !completionHandler?:completionHandler(nil, error);
    }];
}

+ (id)GET:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil);
            }else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error);
            }
        }


    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@, %@", task.currentRequest.URL.absoluteString, error);
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
            if([netErrorModel.status integerValue] == 401){
                [[Siter sharedInstance] refreshacTokenwithCallbcak:^(id user, NSError *error) {
                    
                    if(error==nil){
                        [[[Siter sharedInstance] sessionWithDefaultAuthorization] GET:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                    NSNumber *code = responseObject[@"code"];
                                    if(code==nil){
                                        NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                        !completionHandler?:completionHandler(nil, error);
                                    }else{
                                        if([code intValue]==200){
                                            !completionHandler?:completionHandler(responseObject[@"data"], nil);
                                        }else{
                                            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                            !completionHandler?:completionHandler(nil, error);
                                        }
                                    }
                                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                    !completionHandler?:completionHandler(nil, error);
                                                }];
                    }else {
                        !completionHandler?:completionHandler(nil, error);
                    }
                }];
            }else {
                !completionHandler?:completionHandler(nil, error);
            }
    }];
}

+ (id)DELETE:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] DELETE:path parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil);
            }else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@, %@", task.currentRequest.URL.absoluteString, error);
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
            if([netErrorModel.status integerValue] == 401){
                [[Siter sharedInstance] refreshacTokenwithCallbcak:^(id user, NSError *error) {
                    
                    if(error==nil){
                        [[[Siter sharedInstance] sessionWithDefaultAuthorization] DELETE:path parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                            NSNumber *code = responseObject[@"code"];
                            if(code==nil){
                                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                !completionHandler?:completionHandler(nil, error);
                            }else{
                                if([code intValue]==200){
                                    !completionHandler?:completionHandler(responseObject[@"data"], nil);
                                }else{
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error);
                                }
                            }
                                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                    !completionHandler?:completionHandler(nil, error);
                                                }];
                    }else {
                        !completionHandler?:completionHandler(nil, error);
                    }
                }];
            }else {
                !completionHandler?:completionHandler(nil, error);
            }
    }];
}


+ (id)PUT:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] PUT:path parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil);
            }else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@, %@", task.currentRequest.URL.absoluteString, error);
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
            if([netErrorModel.status integerValue] == 401){
                [[Siter sharedInstance] refreshacTokenwithCallbcak:^(id user, NSError *error) {
                    
                    if(error==nil){
                        [[[Siter sharedInstance] sessionWithDefaultAuthorization] PUT:path parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                            NSNumber *code = responseObject[@"code"];
                            if(code==nil){
                                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                !completionHandler?:completionHandler(nil, error);
                            }else{
                                if([code intValue]==200){
                                    !completionHandler?:completionHandler(responseObject[@"data"], nil);
                                }else{
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error);
                                }
                            }
                                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                    !completionHandler?:completionHandler(nil, error);
                                                }];
                    }else {
                        !completionHandler?:completionHandler(nil, error);
                    }
                }];
            }else {
                !completionHandler?:completionHandler(nil, error);
            }
    }];
}


+ (id)POST:(NSString *)path parameters:(id)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] POST:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil);
            }else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@, %@", task.currentRequest.URL.absoluteString, error);
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
            if([netErrorModel.status integerValue] == 401){
                [[Siter sharedInstance] refreshacTokenwithCallbcak:^(id user, NSError *error) {
                    
                    if(error==nil){
                        [[[Siter sharedInstance] sessionWithDefaultAuthorization] POST:path parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                            NSNumber *code = responseObject[@"code"];
                            if(code==nil){
                                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                !completionHandler?:completionHandler(nil, error);
                            }else{
                                if([code intValue]==200){
                                    !completionHandler?:completionHandler(responseObject[@"data"], nil);
                                }else{
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error);
                                }
                            }
                                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                    !completionHandler?:completionHandler(nil, error);
                                                }];
                    }else {
                        !completionHandler?:completionHandler(nil, error);
                    }
                }];
            }else {
                !completionHandler?:completionHandler(nil, error);
            }
    }];
}


+ (id)PATCH:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id, NSError *))completionHandler {
    return [[[Siter sharedInstance] sessionWithDefaultAuthorization] PATCH:path parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", task.currentRequest.URL.absoluteString);
        NSNumber *code = responseObject[@"code"];
        if(code==nil){
            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
            !completionHandler?:completionHandler(nil, error);
        }else{
            if([code intValue]==200){
                !completionHandler?:completionHandler(responseObject[@"data"], nil);
            }else{
                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                !completionHandler?:completionHandler(nil, error);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@, %@", task.currentRequest.URL.absoluteString, error);
        NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
        ErrorModel *netErrorModel = [[ErrorModel alloc] initWithString:errResponse error:nil];
            if([netErrorModel.status integerValue] == 403){
                [[Siter sharedInstance] refreshacTokenwithCallbcak:^(id user, NSError *error) {
                    
                    if(error==nil){
                        [[[Siter sharedInstance] sessionWithDefaultAuthorization] PATCH:path parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                            NSNumber *code = responseObject[@"code"];
                            if(code==nil){
                                NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:-1 userInfo:nil];
                                !completionHandler?:completionHandler(nil, error);
                            }else{
                                if([code intValue]==200){
                                    !completionHandler?:completionHandler(responseObject[@"data"], nil);
                                }else{
                                    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:[code intValue] userInfo:nil];
                                    !completionHandler?:completionHandler(nil, error);
                                }
                            }
                                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                    !completionHandler?:completionHandler(nil, error);
                                                }];
                    }else {
                        !completionHandler?:completionHandler(nil, error);
                    }
                }];
            }else {
                !completionHandler?:completionHandler(nil, error);
            }
    }];
}
@end
